import { GSprite } from "../../../../com/mui/controls/GSprite";
import { FahunItemInfo } from "./FahunItemInfo";
import { ItemConfig } from "../../resource/ItemConfig";
import { ECategory } from "../../../../Message/Public/ECategory";
import { EProp } from "../../../../Message/Public/EProp";
import { ItemData } from "../../resource/info/ItemData";
import { Cache } from "../../cache/Cache";
import { GlobalStyle } from "../../../common/global/GlobalStyle";
import { HTMLUtil } from "../../../../com/gengine/utils/HTMLUtil";
import { ObjCreate } from "../common/ObjCreate";
import { ImagesConst } from "../../resource/ImagesConst";
import { FahunItem } from "./FahunItem";
import { DeployPanelItem } from "../equipment/deploy/DeployPanelItem";
import { Language } from "../../../../extend/language/Language";
import { FahunStarPanel } from "./FahunStarPanel";
import { GlobalSkin } from "../../../component/skin/GlobalSkin";
import { LoaderHelp } from "../../../common/display/LoaderHelp";
import { ResFileConst } from "../../resource/ResFileConst";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { EAmuletSpiritOper } from "../../../../Message/Game/EAmuletSpiritOper";
import { MsgManager } from "../../manager/MsgManager";
import { GameDefConfig } from "../../resource/GameDefConfig";
import { Alert } from "../../../../com/mui/controls/Alert";
import { AmuletSpiritConfig } from "../../resource/AmuletSpiritConfig";
import { ItemsUtil } from "../shortcuts/item/ItemsUtil";
import { FilterConst } from "../../../component/gconst/FilterConst";
import { GTextFiled } from "../../../../com/mui/controls/GTextFiled";
import { GButton } from "../../../../com/mui/controls/GButton";
import { TAmuletSpiritConfig } from "../../../../Message/Db/Tables/TAmuletSpiritConfig";
import { _EAmuletSpiritGuideUpgrade } from "../../../../EAmuletSpiritOper/_EAmuletSpiritGuideUpgrade";
import { _EAmuletSpiritUpgrade } from "../../../../EAmuletSpiritOper/_EAmuletSpiritUpgrade";
type int = number;
//class FahunUpgradePanel
    
    export  class FahunUpgradePanel extends GSprite
    {
        constructor()
        {
            super();
            this._bestInfo = new FahunItemInfo();
            var loc1=ItemConfig.instance.getItemByType(ECategory._ECategoryProp, EProp._EPropAmuletSpiritUpgradeStone);
            if (loc1) 
            {
                this._materialItemData = new ItemData(loc1.item.code);
            }
            return;
        }

        public moneyUpdate(): void
        {
            var loc1=null;
            this._isMoneyEnough = Cache.instance.role.moneyCoin >= this._needMoney;
            if (this._isMoneyEnough) 
            {
                loc1 = GlobalStyle.color1;
            }
            else 
            {
                loc1 = GlobalStyle.color7;
            }
            this._costTxt.htmlText = HTMLUtil.addColor(this._needMoney.toString(), loc1);
            return;
        }

        protected /* override */ createDisposedChildrenImpl(): void
        {
            super.createDisposedChildrenImpl();
            ObjCreate.createGImageBitmap(ImagesConst.LongBg, 20, 30, this);
            ObjCreate.createGImageBitmap("Label_jipingyulang.png", 6, 60, this);
            this._bestItem = new FahunItem();
            this._bestItem.move(26, 10);
            addChild(this._bestItem);
            this._cItem = new FahunItem();
            this._cItem.move(80, 85);
            addChild(this._cItem);
            this._cItem.addEventListener(flash.events.MouseEvent.CLICK, this.onItemClickHandler);
            ObjCreate.createGImageBitmap("equipment_recast_recast.png", 140, 86, this);
            this._materialItem = new DeployPanelItem(0, true);
            this._materialItem.label = Language.getString(58013);
            this._materialItem.x = 190;
            this._materialItem.y = 90;
            this.addChild(this._materialItem);
            ObjCreate.createBg(35, 155, 235, 105, this);
            ObjCreate.createGTextField(Language.getString(58022), 125, 160, 220, 20, this, GlobalStyle.textFormat6);
            ObjCreate.createGTextField(Language.getString(58023), 205, 160, 220, 20, this, GlobalStyle.textFormat6);
            ObjCreate.createGTextField(Language.getString(58014), 50, 180, 220, 20, this, GlobalStyle.textFormat6);
            ObjCreate.createGTextField(Language.getString(58015), 50, 205, 220, 20, this, GlobalStyle.textFormat6);
            this._levelTxtTxt = ObjCreate.createGTextField("", 125, 180, 50, 20, this, GlobalStyle.textFormat6);
            this._levelTxtTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this._wxTxtTxt = ObjCreate.createGTextField("", 125, 205, 50, 20, this, GlobalStyle.textFormat6);
            this._wxTxtTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this._nLevelTxtTxt = ObjCreate.createGTextField("", 205, 180, 50, 20, this, GlobalStyle.textFormat6);
            this._nLevelTxtTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this._nWxTxtTxt = ObjCreate.createGTextField("", 205, 205, 50, 20, this, GlobalStyle.textFormat6);
            this._nWxTxtTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this._starPanel = new FahunStarPanel();
            this._starPanel.x = 30;
            this._starPanel.y = 230;
            this.addChild(this._starPanel);
            this._upgradeBtn = ObjCreate.createGButton(Language.getString(58011), 103, 310, 92, 28, this, GlobalSkin.ButtonRed);
            this._upgradeBtn.name = "fahunUpgradeBtn";
            this._upgradeBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onUpgradeBtnClickHandler);
            this._costSprite = new Object /* flash.display.Sprite */();
            this._costSprite.x = 175;
            this._costSprite.y = 365;
            this.addChild(this._costSprite);
            ObjCreate.createLabel(Language.getString(60322), 0, 0, 100, 20, flash.text.TextFormatAlign.LEFT, this._costSprite, true, 15859633);
            ObjCreate.createBg(34, 1, 75, 18, this._costSprite, ImagesConst.DisabledBg);
            ObjCreate.createBitmap(ImagesConst.Tongqian_small, 87, 3, this._costSprite);
            this._costTxt = ObjCreate.createGTextField("0", 34, 1, 50, 20, this._costSprite);
            this._costTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
            LoaderHelp.addResCallBack(ResFileConst.fahun, this.onLoadHandler);
            return;
        }

        /* internal  */onLoadHandler(): void
        {
            ObjCreate.createBitmap(ImagesConst.FahunULabel, 15, 135, this);
            ObjCreate.createBitmap(ImagesConst.FahunLDesc, 20, 265, this);
            return;
        }

        protected /* override */ disposeImpl(arg1: Boolean=true): void
        {
            return;
        }

        /* internal  */onItemClickHandler(arg1: Object /* flash.events.Event */): void
        {
            this.updateByFahunItemInfo(null);
            return;
        }

        /* internal  */onUpgradeBtnClickHandler(arg1: Object /* flash.events.Event */): void
        {
            var e: Object /* flash.events.Event */;
            var mrArray: Array<any>;
            var tmp: int;
            var itemData: ItemData;
            var i: int;
            var onSelect1: Function;

            var loc1;
            mrArray = null;
            tmp = 0;
            itemData = null;
            i = 0;
            e = arg1;
            if (this._fahunItemInfo) 
            {
                if (Cache.instance.fahun.isGuiding) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.FahunOper, {"index": this._cItem.fahunItemInfo.index, "oper": _EAmuletSpiritGuideUpgrade, "materials": mrArray}));
                }
                else 
                {
                    if (!this._fahunItemInfo.isEnable) 
                    {
                        MsgManager.showRollTipsMsg(Language.getStringByParam(58029, GameDefConfig.instance.getFahunMW(this._fahunItemInfo.index)));
                        return;
                    }
                    if (this._fahunItemInfo.isMaxLevel) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(58028));
                        return;
                    }
                    if (this._fahunItemInfo.isNeedBreak) 
                    {
                        onSelect1 = (arg1: int): void
                        {
                            if (arg1 == Alert.OK) 
                            {
                                Dispatcher.dispatchEvent(new DataEvent(EventName.FahunShowTab, {"operIndex": 1, "itemIndex":(_fahunItemInfo.index - 1)}));
                            }
                            return;
                        }
                        Alert.show(Language.getString(58019), null, Alert.OK | Alert.CANCEL, null, onSelect1);
                        return;
                    }
                    if (!this._isMoneyEnough) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(60121));
                        return;
                    }
                    if (!this._isMaterialEnough) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(58020));
                        return;
                    }
                    mrArray = [];
                    i = 0;
                    while (i < this._itemArray.length) 
                    {
                        itemData = this._itemArray[i];
                        mrArray.push(itemData.uid);
                        tmp = tmp + itemData.itemAmount;
                        if (tmp >= this._needCount) 
                        {
                            break;
                        }
                        ++i;
                    }
                    Dispatcher.dispatchEvent(new DataEvent(EventName.FahunOper, {"index": this._cItem.fahunItemInfo.index, "oper": _EAmuletSpiritUpgrade, "materials": mrArray}));
                }
            }
            return;
        }

        public clear(): void
        {
            this._materialItem.itemData = null;
            this._materialItem.filters = null;
            this._bestItem.fahunItemInfo = null;
            this._cItem.fahunItemInfo = null;
            this._levelTxtTxt.text = "";
            this._wxTxtTxt.text = "";
            this._nLevelTxtTxt.text = "";
            this._nWxTxtTxt.text = "";
            this._starPanel.num = 0;
            this._costTxt.text = "0";
            return;
        }

        public updateByFahunItemInfo(arg1: FahunItemInfo): void
        {
            this._fahunItemInfo = arg1;
            this._cItem.fahunItemInfo = this._fahunItemInfo;
            if (arg1) 
            {
                this._bestInfo.copy(arg1);
                this._bestInfo.level = this._maxLevel;
                this._bestItem.fahunItemInfo = this._bestInfo;
                this._starPanel.num = arg1.starNum;
                this._configInfo = AmuletSpiritConfig.instance.getConfigByLevel(arg1.level);
                this._nConfigInfo = AmuletSpiritConfig.instance.getConfigByLevel(arg1.level + 1);
                if (this._configInfo) 
                {
                    this._levelTxtTxt.text = this._configInfo.level.toString();
                    this._wxTxtTxt.text = this._configInfo.wuxingAdd.toString();
                }
                else 
                {
                    this._levelTxtTxt.text = "0";
                    this._wxTxtTxt.text = "0";
                }
                if (this._nConfigInfo) 
                {
                    this._needCount = this._nConfigInfo.upgradeMaterials;
                    this._needMoney = this._nConfigInfo.upgradeMoney;
                    this._nLevelTxtTxt.text = this._nConfigInfo.level.toString();
                    this._nWxTxtTxt.text = this._nConfigInfo.wuxingAdd.toString();
                }
                else 
                {
                    this._nLevelTxtTxt.text = "";
                    this._nWxTxtTxt.text = "";
                }
                this._materialItem.itemData = this._materialItemData;
                this._materialItem.amount = this._needCount;
                this.materialUpdate();
                this.moneyUpdate();
            }
            else 
            {
                this.clear();
            }
            return;
        }

        public materialUpdate(): void
        {
            this._itemCount = Cache.instance.pack.backPackCache.getItemCountByCategoryAndType(ECategory._ECategoryProp, EProp._EPropAmuletSpiritUpgradeStone);
            this._itemArray = Cache.instance.pack.backPackCache.getItemsByJudge(ItemsUtil.isFahunUpgradeItem);
            this._isMaterialEnough = this._itemCount >= this._needCount;
            if (this._cItem.fahunItemInfo) 
            {
                if (this._isMaterialEnough) 
                {
                    this._materialItem.filters = null;
                }
                else 
                {
                    this._materialItem.filters = [FilterConst.colorFilter];
                }
            }
            return;
        }

        /* internal */ /* var */_bestItem: FahunItem;

        /* internal */ /* var */_cItem: FahunItem;

        /* internal */ /* var */_materialItem: DeployPanelItem;

        /* internal */ /* var */_levelTxtTxt: GTextFiled;

        /* internal */ /* var */_wxTxtTxt: GTextFiled;

        /* internal */ /* var */_nLevelTxtTxt: GTextFiled;

        /* internal */ /* var */_nWxTxtTxt: GTextFiled;

        /* internal */ /* var */_starPanel: FahunStarPanel;

        /* internal */ /* var */_upgradeBtn: GButton;

        /* internal */ /* var */_costSprite: Object /* flash.display.Sprite */;

        /* internal */ /* var */_costTxt: GTextFiled;

        /* internal */ /* var */_bestInfo: FahunItemInfo;

        /* internal */ /* var */_materialItemData: ItemData;

        /* internal */ /* var */_needCount: int;

        /* internal */ /* var */_itemCount: int;

        /* internal */ /* var */_maxLevel: int=100;

        /* internal */ /* var */_fahunItemInfo: FahunItemInfo;

        /* internal */ /* var */_configInfo: TAmuletSpiritConfig;

        /* internal */ /* var */_nConfigInfo: TAmuletSpiritConfig;

        /* internal */ /* var */_isMoneyEnough: Boolean;

        /* internal */ /* var */_isMaterialEnough: Boolean;

        /* internal */ /* var */_needMoney: int;

        /* internal */ /* var */_itemArray: Array<any>;
    }
